<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Events extends OC_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->model('home/home_model');
        $this->load->model('events/events_model');
        $this->load->library('recaptcha');
    }

    function _remap($method, $params = array())
    {
        parent::_remap($method, $params);

        if (method_exists($this, $method))
            return call_user_func_array(array($this, $method), $params);
        else
            show_404();
    }

    function init_header()
    {
        $this->header['meta'] = array(
            'description' => array('name' => 'description', 'content' => 'Haan Kreasi Ibu Juara'),
            'keywords' => array('name' => 'keywords', 'content' => 'Haan Kreasi Ibu Juara, recipes, kreasi ibu, kreatif, sajian  kreatif'),
        );
    }

    public function index()
    {
        $this->init_header();
        $this->load->library('pagination');
        $this->load->helper("url");

        $config                     = array();
        $config["base_url"]         = base_url() . "events/index";
        $config["total_rows"]       = $this->events_model->record_count();
        $config["per_page"]         = 4;
        $config["uri_segment"]      = 3;
        $config['cur_tag_open']     = '&nbsp;<li>';
        $config['cur_tag_close']    = '</li>';
        $config['next_link']        = 'Next';
        $config['prev_link']        = 'Previous';
        $config['num_tag_open']     = '<li>';
        $config['num_tag_close']    = '</li>';
        $config['prev_link']        = '&lt;';
        $config['prev_tag_open']    = '<li>';
        $config['prev_tag_close']   = '</li>';
        $config['first_link']       = 'First';
        $config['first_tag_open']   = '<li>';
        $config['first_tag_close']  = '</li>';
        $config['next_link']        = '&gt;';
        $config['next_tag_open']    = '<li>';
        $config['next_tag_close']   = '</li>';
        $config['cur_tag_open']     = '<li class="active"><span>';
        $config['cur_tag_close']    = '</span></li>';
        $config['full_tag_open']    = '<li>';
        $config['full_tag_close']   = '</li>';

        $this->pagination->initialize($config);
 
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        $this->header["results"]            = $this->events_model->fetch_countries($config["per_page"], $page);
        $this->header["links"]              = $this->pagination->create_links();
        $this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['socmed_home']        = $this->home_model->get_socmed();
        $this->header['top_nav']            = 'events';
        $this->header['widget_home']    = $this->home_model->get_widget();
        $this->header['widget_footer']  = $this->home_model->get_widget(2);
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');
        $this->header['event_home']     = $this->home_model->get_event();
        $this->view('events/events_view');
    }

    public function detail_events(){
        $this->init_header();
        $e_id = $this->uri->segment(3);

        $this->header['top_nav']            = 'events';
        $this->header['events']             = $this->events_model->get_event($e_id);
        $this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['comments']           = $this->events_model->get_comments($e_id);
        $this->header['recaptcha_html']     = $this->recaptcha->recaptcha_get_html();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['socmed_home']    = $this->home_model->get_socmed();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');
        $this->header['widget_home']    = $this->home_model->get_widget();
        $this->header['widget_footer']  = $this->home_model->get_widget(2);
        $this->header['event_home']     = $this->home_model->get_event();
        $this->view('events/detail_events_view');
    }

    public function do_comment()
    {
        $this->recaptcha->recaptcha_check_answer();
        $title = $this->input->post('r_name');
        if($this->input->post('submit'))
        {
            unset($_POST['recaptcha_challenge_field']);
            unset($_POST['recaptcha_response_field']);
            unset($_POST['submit']);
            unset($_POST['e_name']);

            if($this->recaptcha->getIsValid()){
                $_POST['ce_created_date'] = date('Y-m-d H:i:s');
                $do_add = $this->events_model->do_comment($_POST);
                if($do_add){
                    redirect('events/detail/'.slugify($title).'/'.$this->input->post('e_id'), 'refresh');
                }else{
                    redirect('events/detail/'.slugify($title).'/'.$this->input->post('e_id'), 'refresh');
                }
            }else{
                redirect('events/detail/'.slugify($title).'/'.$this->input->post('e_id'), 'refresh');
            }
        }
    }

}

/* End of file events.php */
/* Location: ./applications/module_front/events/controllers/events.php */